package da.metricscraft.client.threads;

import java.util.ArrayList;
import java.util.HashMap;

import org.w3c.dom.NodeList;

public class PlayerXMLLoader extends XMLLoader<HashMap<String, ArrayList<PlayerXMLLoader.PlayerStatsStruct>>> {
	public enum PlayerDataType{
		LIFETIME,
		RUNTIME,
		SPRINTTIME,
		SWIMTIME,
		BOATTIME,
		CROUCHTIME,
		UNDERGROUNDTIME,
		CHUNKS_DISCOVERED, 
		CHESTS_OPENED,
		MINED,
		FINE_MINED,
		JUMPS, 
		MININGTIME, 
		BUILT, 
		FITTING_ITEMS_USED, 
		DAMAGE_TAKEN, 
		DAMAGE_TAKEN_LAVA,
		DAMAGE_TAKEN_CONTACT,
		DAMAGE_TAKEN_ENTITY_ATTACK,
		DAMAGE_TAKEN_PROJECTILE,
		DAMAGE_TAKEN_FALL,
		DAMAGE_TAKEN_FIRE_TICK,
		DAMAGE_TAKEN_BLOCK_EXPLOSION,
		DAMAGE_TAKEN_ENTITY_EXPLOSION,
		DAMAGE_TAKEN_STARVATION,
		DAMAGE_TAKEN_DROWNING,
		HEALTH_REGAINED, 
		KILLS, 
		DEVELOP, 
		BUTTONS_PRESSED, 
		DEVELOP_REMOVE,
		DEATHS;
	}
	public static class PlayerStatsStruct implements Comparable<PlayerStatsStruct>{
		public PlayerDataType type;
		private float value;
		public void setValue(float value){
			this.value = value;
		}
		public int intValue(){
			return (int)value;
		}
		public float floatValue(){
			return value;
		}
		public PlayerStatsStruct(PlayerDataType type, float value){
			this.type = type;
			this.value = value;
		}
		@Override
		public int compareTo(PlayerStatsStruct arg0) {
			// TODO Auto-generated method stub
			return type.name().compareTo(arg0.type.name());
		}
		public String toString(){
			return type.name() + ": " + value;
		}
	}
	private HashMap<String, ArrayList<PlayerStatsStruct>> list = new HashMap<String, ArrayList<PlayerStatsStruct>>();
	public PlayerXMLLoader(LoaderTypes type) {
		super(type);
		// TODO Auto-generated constructor stub
	}

	@Override
	public void run() {
		// TODO Auto-generated method stub
		while(true){
			if (path != ""){
				readXML();
				NodeList playerList = xmldoc.getElementsByTagName("player");
				for (int j = 0; j < playerList.getLength(); j++){
					NodeList nl = playerList.item(j).getChildNodes();
					ArrayList<PlayerStatsStruct> playerStats = new ArrayList<PlayerStatsStruct>();
					for(int i = 0; i < nl.getLength(); i++){

						playerStats.add(new PlayerStatsStruct(PlayerDataType.valueOf(nl.item(i).getNodeName()), Integer.parseInt(nl.item(i).getTextContent())));
					}
					list.put(playerList.item(j).getAttributes().getNamedItem("name").getTextContent(), playerStats);
				}
				data = list;
				dispatch();

				path = "";
			}else{
				try {
					synchronized(this){
						this.wait();
					}
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
	}

}
